package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.adapter.PurSupplierListAdapter;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.component.MyListBottom;
import com.aoxon.cargo.component.MyRadioButton;
import com.aoxon.cargo.keydown.ExitApplication;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.CerSupplierService;
import com.aoxon.cargo.service.HotSupplierService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.google.gson.reflect.TypeToken;

/**
 * 采购端供应商列表
 * 
 * @author ZZJ
 * 
 */
public class PurSupplierListActivity extends NewBaseActivity implements
		MyRadioButton.OnCheckedListener, NormalDataLoader.OnLoadDataListener {

	private int visibleLastIndex;
	private int startIndex = 0;
	private int length = 20;
	private int loadType = 0;
	private boolean isReachLast = false;

	private TextView ivSearchSupButton;
	private ImageView ivGPSButton;

	private TextView tvTitle;

	private ListView lvSupplierList;

	private PurSupplierListAdapter adapter;

	private MyListBottom myListBottom;
	private MyRadioButton myRadioButton;

	private ExitApplication exitApplication = new ExitApplication(this);
	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	private List<Supplier> suppliers = new ArrayList<Supplier>();
	private HotSupplierService hotSupplierService = (HotSupplierService) MyServiceFactory
			.getInstance(MyServiceFactory.HOT_SUPPLIER);
	private CerSupplierService cerSupplierService = (CerSupplierService) MyServiceFactory
			.getInstance(MyServiceFactory.CER_SUPPLIER);



	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.pur_supplier_list;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		
		ivSearchSupButton = (TextView) findViewAndSetOnClick(R.id.tvPurIndexSearchButton);
		ivGPSButton = (ImageView) findViewAndSetOnClick(R.id.ivPurIndexGPS);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);

		lvSupplierList = (ListView) findViewById(R.id.lvPurSullierList);

		tvTitle.setText("商家");
		ivSearchSupButton.setText("找商家");
		myListBottom = new MyListBottom(this, lvSupplierList);
		myRadioButton = new MyRadioButton(this, "按人气排序", "按认证排序");
		myRadioButton.setOnCheckedChanged(this);

		normalDataLoader.setOnloadDataListener(this);

		adapter = new PurSupplierListAdapter(this, suppliers, false);
		lvSupplierList.setAdapter(adapter);
		adapter.notifyDataSetChanged();

		lvSupplierList.setOnScrollListener(myScrollListener);
		lvSupplierList.setOnItemClickListener(myItemClickListener);

		normalDataLoader.load();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (view.getId()) {
		
		case R.id.ivPublicTitleOther: 
			Log.v(" onclick ", " id orther");
			break;
		case R.id.ivPurIndexGPS:
			Log.v(" onclick ", " ivPurIndexGPS");
			break;
		
		default:
			intent = new Intent(getBaseContext(),
					PurSupplierSearch2Activity.class);
//					PurSupplierSearchActivity.class);
			startActivity(intent);
			break;
		}
	}

		@Override
		public void checkOne() {
			// TODO Auto-generated method stub
			loadType = 0;
			startIndex = 0;
			suppliers.clear();
			isReachLast = false;
			adapter.notifyDataSetChanged();
			normalDataLoader.load();
		}

		@Override
		public void checkTwo() {
			// TODO Auto-generated method stub
			loadType = 1;
			startIndex = 0;
			suppliers.clear();
			isReachLast = false;
			adapter.notifyDataSetChanged();
			normalDataLoader.load();
		}

		OnScrollListener myScrollListener = new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_FLING:
					adapter.setBusy(true);
					break;
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					adapter.setBusy(false);
					break;
				case OnScrollListener.SCROLL_STATE_IDLE:
					int lastIndex = adapter.getCount();
					adapter.setBusy(false);

					// if (visibleLastIndex == lastIndex && !isLoading &&
					// !isReachLast) {
					// 滑动到列表底部时便加载数据
					// }

					// 滑动到列表的2/3时便加载数据
					if (visibleLastIndex >= (int) (lastIndex * 2 / 3)
							&& !isReachLast) {
						normalDataLoader.load();
					}
					break;
				default:
					break;
				}
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				visibleLastIndex = firstVisibleItem + visibleItemCount - 1;

			}
		};

		/**
		 * 选项点击监听
		 */
		OnItemClickListener myItemClickListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if ((adapter.getCount() != position)) {
					try {
						Intent intent = new Intent(PurSupplierListActivity.this,
								PurSupplierDetailsActivity.class);
						intent.putExtra("strSupplier",
								DataUtil.gson.toJson(suppliers.get(position)));
						System.out.print(DataUtil.gson.toJson(suppliers
								.get(position)));
						startActivity(intent);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

		};

		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			return exitApplication.onKeyDown(keyCode, event);
		}


		@Override
		public void beforeLoad() {
			// TODO Auto-generated method stub
			myListBottom.showProgressBar();
			myListBottom.setText("加载中...");
		}

		@Override
		public void toLoad(Message msg) throws Exception {
			// TODO Auto-generated method stub
			// isLoading = true;
			normalDataLoader.loading(true);
			GsonBean gsonBean = null;
			if (0 == loadType) {
				gsonBean = hotSupplierService.execute(startIndex, length);
			} else {
				gsonBean = cerSupplierService.execute(startIndex, length);
			}
			if (CheckStateUtil.check(gsonBean)) {
				List<Supplier> supplierList = DataUtil.gson.fromJson(
						gsonBean.getStrJson(), new TypeToken<List<Supplier>>() {
						}.getType());
				startIndex += length;
				suppliers.addAll(supplierList);
				msg.what = MyStateUtil.LIST_CHANGE;
			} else {
				// 查找不到数据的情况
				isReachLast = true;
				msg.what = MyStateUtil.LIST_IS_REACH_LAST;
			}
		}

		@Override
		public void afterLoad(int state) {
			// TODO Auto-generated method stub
			myListBottom.hideProgressBar();
			switch (state) {
			case MyStateUtil.LIST_CHANGE:
				Log.i("listChange",
						"------------------listChange--------------------");
				myListBottom.setText("向上拉加载数据");
				break;
			case MyStateUtil.NOT_NET_CONNECTION:
				myListBottom.setText("无网络连接～");
				break;
			case MyStateUtil.LIST_IS_REACH_LAST:
				myListBottom.setText("没有相应数据咯～");
				break;
			case MyStateUtil.ERROR_NET_CONNECTION:
				myListBottom.setText("向上拉加载数据");
				break;
			default:
				break;
			}
			adapter.notifyDataSetChanged();
		}

}
